#!/usr/bin/perl

use Configs;
use XML::LibXML;
use Digest::MD5 qw(md5_hex);

eval "use Image::Magick";
if ($@) {
	$bMagick = false;
} else {
	$bMagick = true;
}

#eval "use GD";
#if ($@) {
#	$bGD = false;
#} else {
#	$bGD = true;
#}

if ($ENV{'SERVER_SOFTWARE'}) {
	print "Content-type: text/html\n\n";
	print "<HTML><BODY>";
}


my $content = '';

print "Started\n";

my @mon = ('Jan','Fed','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec');

my $parser = XML::LibXML->new();
my $tree = $parser->parse_file($feedurl);
my $root = $tree->getDocumentElement;

my @entries = $root->getChildrenByTagName('entry');
$cnt = @entries;
print "cnt: " . $cnt . "\n";

foreach my $entry (@entries) {
	print "\n\n".$entry."\n";
	
	my @flvthumb_url;
	my @flv_url;
	my @flv_length;
	my @flv_title;
	my @flv_size;
	
	my @mpgthumb_url;
	my @mpg_url;
	my @mpg_length;
	my @mpg_title;
	my @mpg_size;
	
	my @other_url;
	my @other_title;
	
	#my @post_meta;
	
	my $flv_cnt = 0;
	my $mpg_cnt = 0;
	my $other_cnt = 0;
	
	my $twotwofiveseven_url = '';
	my $sponsor_url = '';
	my $sponsor_title = '';
	my $buy_title = '';
	my $buy_image = '';
	my $buy_url = '';
	my $playlist_url = '';


	my $this_id = $entry->getChildrenByTagName('id');
	my $this_updated = $entry->getChildrenByTagName('updated');
	my $this_published = $entry->getChildrenByTagName('published');
	my $this_title = $entry->getChildrenByTagName('title');
	my $this_content = $entry->getChildrenByTagName('content');

	# Legacy var names
	#$video_id = crypt($this_id,$this_id);
	$video_id = md5_hex($this_id);
	$video_id =~ s/\s//gi;
	$video_id =~ s/\.//gi;
	$video_id =~ s/\,//gi;
	$video_id =~ s/\///gi;
	$video_id =~ s/\\//gi;
	$title = $this_title;
	$description = $this_content;

	my @authors = $entry->getChildrenByTagName('author');
	my @categories = $entry->getChildrenByTagName('category');
	my @links = $entry->getChildrenByTagName('link');
	my @contributors = $entry->getChildrenByTagName('contributor');

	if ($this_updated =~ /([0-9]{4})-([0-9]{2})-([0-9]{2})T[0-9]{2}:[0-9]{2}:[0-9]{2}Z/i) {
		$this_updated = $3 .' '. $mon[$2] .' '. $1;
	}
	if ($this_published =~ /([0-9]{4})-([0-9]{2})-([0-9]{2})T[0-9]{2}:[0-9]{2}:[0-9]{2}Z/i) {
		$this_published = $3 .' '. $mon[$2] .' '. $1;
	}

	print "this_id: " . $this_id ."\n";
	print "this_updated: " . $this_updated ."\n";
	print "this_published: " . $this_published ."\n";
	print "this_title: " . $this_title ."\n";
	print "this_content: " . $this_content ."\n";

	my %this_authors;
	foreach my $author (@authors) {
		$this_authors{$author->getChildrenByTagName('name')} = $author->getChildrenByTagName('uri');
		print "author: " . $author->getChildrenByTagName('name') ."\n";
	}

	my @this_categories;
	foreach my $category (@categories) {
		push(@this_categories, $category->getAttribute('term'));
		print "category: " . $category->getAttribute('term') ."\n";
	}

	my @this_links;
	foreach my $link (@links) {
		# 2257
		if ($link->getAttribute('title') eq "2257") {
			$twotwofiveseven_url = $link->getAttribute('href');
			
		# MPEGs
		} elsif ($link->getAttribute('type') eq "video/mpeg" && $link->getAttribute('rel') eq "enclosure") {
			$mpg_size[$mpg_cnt] = $link->getAttribute('length');
			$mpg_length[$mpg_cnt] = $link->getAttribute('length');
			my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime($mpg_length[$mpg_cnt]);
			if ($min>0) {
				$mpg_length[$mpg_cnt] = $min."m ".$sec."s";
			} else {
				$mpg_length[$mpg_cnt] = $sec."s";
			}
			$mpg_url[$mpg_cnt] = $link->getAttribute('href');
			$mpg_title[$mpg_cnt] = $link->getAttribute('title');
			$mpgthumb_url[$x] = '';
			$mpg_cnt++;
			
		# FLVs
		} elsif ($link->getAttribute('type') eq "video/x-flv" && $link->getAttribute('rel') eq "enclosure") {
			$flv_size[$flv_cnt] = $link->getAttribute('length');
			$flv_length[$flv_cnt] = $link->getAttribute('length');
			my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime($flv_length[$flv_cnt]);
			if ($min>0) {
				$flv_length[$flv_cnt] = $min."m ".$sec."s";
			} else {
				$flv_length[$flv_cnt] = $sec."s";
			}
			$flv_url[$flv_cnt] = $link->getAttribute('href');
			$flv_title[$flv_cnt] = $link->getAttribute('title');
			$flvthumb_url[$x] = '';
			$flv_cnt++;
			
		# Sponsor Link
		} elsif ($link->getAttribute('type') eq "text/html" && $link->getAttribute('rel') eq "alternate") {
			$sponsor_url = $link->getAttribute('href');
			$sponsor_title = $link->getAttribute('title');
			
		# Buy DVD Link
		} elsif ($link->getAttribute('title') =~ /Buy DVD/i && $buy_url eq '' && $link->getAttribute('type') eq "text/html" && $link->getAttribute('rel') eq "related") {
			$buy_title = $link->getAttribute('title');
			$buy_url = $link->getAttribute('href');
		
		# XSPF Playlist
		} elsif ($link->getAttribute('title') eq 'playlist' && $link->getAttribute('type') eq "text/xml" && $link->getAttribute('rel') eq "related") {
			$playlist_url = $link->getAttribute('href');
		
		# Other Links
		} else {
			if (substr($link->getAttribute('title'), 0, 5) ne 'image' && $link->getAttribute('title') ne 'watermark' && $link->getAttribute('title') ne 'logo') {
				$other_url[$other_cnt] = $link->getAttribute('href');
				$other_title[$other_cnt] = $link->getAttribute('title');
				$other_cnt++;
			}
		}
		
		
	}

	# Match Images to Links
	foreach my $link (@links) {
		for ($x=0;$x<$flv_cnt;$x++) {
			if ($link->getAttribute('title') eq 'image['.$flv_title[$x].']' && $link->getAttribute('type') eq "image/jpeg" && $link->getAttribute('rel') eq "related") {
				$flvthumb_url[$x] = $link->getAttribute('href');
			}
		}
		for ($x=0;$x<$mpg_cnt;$x++) {
			if ($link->getAttribute('title') eq 'image['.$mpg_title[$x].']' && $link->getAttribute('type') eq "image/jpeg" && $link->getAttribute('rel') eq "related") {
				$mpgthumb_url[$x] = $link->getAttribute('href');
			}
		}
		if ($link->getAttribute('title') eq 'image['.$buy_title.']' && $link->getAttribute('type') eq "image/jpeg" && $link->getAttribute('rel') eq "related") {
			$buy_image = $link->getAttribute('href');
		}
	}

	my @this_contributors;
	foreach my $contributor (@contributors) {
		push(@this_contributors, $contributor->getChildrenByTagName('name'));
		print "contributor: " . $contributor->getChildrenByTagName('name') ."\n";
	}

	$content .= $this_content;

	$bHasPlaylist = false;
	if ($playlist_url ne '') {
		print "<BR/><b>Playlist</b><BR/>\n";
		print "URL: " . $playlist_url . "<BR/>\n";
		
		$content .= '<DIV CLASS="adultatom_flv_wrap" id="flvplayer_'.$video_id.'_'.$x.'"></DIV>'.$cr;
		$content .= '<DIV CLASS="adultatom_flv_text">';
		$content .= $flv_title[$x] . '&nbsp;&nbsp;['. $flv_length[$x] .']';
		$content .= '</DIV>'.$cr;
		$content .= '<script type="text/javascript">'.$cr;
		$content .= 'var so = new SWFObject("flvplayer.swf", "flvplayer_'.$video_id.'_'.$x.'", "'.$flash_width.'", "'.$flash_height.'", "8", "#FFFFFF");'.$cr;
		#$content .= 'so.useExpressInstall("/flash/expressinstall.swf");'.$cr;
		$content .= 'so.addVariable("logo", "/images/playerlogo.png");'.$cr;
		$content .= 'so.addVariable("file", "'.urlencode($playlist_url).'");'.$cr;
		$content .= 'so.addVariable("showfsbutton", false);'.$cr;
		$content .= 'so.addVariable("displayheight", '.$flash_height.');'.$cr;
		$content .= 'so.addVariable("displaywidth", '.$flash_width.');'.$cr;
		$content .= 'so.addVariable("height", '.$flash_height.');'.$cr;
		$content .= 'so.addVariable("width", '.$flash_width.');'.$cr;
		$content .= 'so.addVariable("link", "'.urlencode($sponsor_url).'");'.$cr;
		$content .= 'so.addVariable("linktarget", "_blank");'.$cr;
		$content .= 'so.addVariable("backcolor", "0xFFFFFF");'.$cr;
		$content .= 'so.addVariable("thumbsinplaylist", "true");'.$cr;
		$content .= 'so.write("flvplayer_'.$video_id.'_'.$x.'");'.$cr;
		$content .= '</script>'.$cr;
		$bHasPlaylist=true;
	}
	
	if (($bHasPlaylist && $playlistplusflvs==1) || $bHasPlaylist==false) {
		$flv_title_cnt = @flv_title;
		if ($flv_title_cnt>0) {
			print "<BR/><b>FLVs</b><BR/>\n";
			for ($x=0;$x<$flv_title_cnt;$x++) {		
				print "Title: " . $flv_title[$x] . "<BR/>\n";
				print "Thumb: " . $flvthumb_url[$x] . "<BR/>\n";
				print "URL: " . $flv_url[$x] . "<BR/>\n";
				print "Length: " . $flv_length[$x] . "<BR/>\n";
				
				$content .= '<DIV CLASS="adultatom_flv_wrap" id="flvplayer_'.$video_id.'_'.$x.'"></DIV>'.$cr;
				$content .= '<DIV CLASS="adultatom_flv_text">';
				$content .= $flv_title[$x] . '&nbsp;&nbsp;['. $flv_length[$x] .']';
				$content .= '</DIV>'.$cr;
				$content .= '<script type="text/javascript">'.$cr;
				$content .= 'var so = new SWFObject("flvplayer.swf", "flvplayer_'.$video_id.'_'.$x.'", "'.$flash_width.'", "'.$flash_height.'", "8", "#FFFFFF");'.$cr;
				#$content .= 'so.useExpressInstall("/flash/expressinstall.swf");'.$cr;
				$content .= 'so.addVariable("logo", "/images/playerlogo.png");'.$cr;
				$content .= 'so.addVariable("file", "'.urlencode($flv_url[$x]).'");'.$cr;
				$content .= 'so.addVariable("image", "'.urlencode($flvthumb_url[$x]).'");'.$cr;
				$content .= 'so.addVariable("showfsbutton", false);'.$cr;
				$content .= 'so.addVariable("displayheight", '.$flash_height.');'.$cr;
				$content .= 'so.addVariable("displaywidth", '.$flash_width.');'.$cr;
				$content .= 'so.addVariable("height", '.$flash_height.');'.$cr;
				$content .= 'so.addVariable("width", '.$flash_width.');'.$cr;
				$content .= 'so.addVariable("link", "'.urlencode($sponsor_url).'");'.$cr;
				$content .= 'so.addVariable("linktarget", "_blank");'.$cr;
				$content .= 'so.addVariable("backcolor", "0xFFFFFF");'.$cr;
				$content .= 'so.write("flvplayer_'.$video_id.'_'.$x.'");'.$cr;
				$content .= '</script>'.$cr;
			}
			$bShowlegal=true;
		}
	}
	
	$mpg_title_cnt = @mpg_title;
	if ($mpg_title_cnt>0){ 
		$content .= '<DIV CLASS="adultatom_mpeg_wrap">';
		print "<BR/><b>MPEGs</b><BR/>\n";
		for ($x=0;$x<$mpg_title_cnt;$x++) {		
			print "Title: " . $mpg_title[$x] . "<BR/>\n";
			print "Thumb: " . $mpgthumb_url[$x] . "<BR/>\n";
			print "URL: " . $mpg_url[$x] . "<BR/>\n";
			print "Length: " . $mpg_length[$x] . "<BR/>\n";
			if ($mpgthumb_url[$x] ne '') {
				if ($bMagick) {
					my $oImageMagick = Image::Magick->new;
					$oImageMagick->Read($mpgthumb_url[$x]);
					$oImageMagick->Resize(width=>$thumb_width, height=>$thumb_height);
					$oImageMagick->Write($thumb_dir.'/'.$video_id.'_'.$x.'.jpg');
				}
			}
			$content .= '<A CLASS="adultatom_mpeg" ID="mpeg_'.$video_id.'" NAME="mpeg_'.$video_id.'" HREF="'.$mpg_url[$x].'" TITLE="'.$mpg_title[$x].'"><IMG SRC="'.$thumb_url.$video_id.'_'.$x.'.jpg" TITLE="'.$mpg_title[$x].'" ALT="'.$mpg_title[$x].'" BORDER=0><BR/>';
			$content .= $mpg_title[$x] . '&nbsp;&nbsp;['. $mpg_length[$x] .']';
			$content .= '</A>';
		}
		$content .= "</DIV>";
		$bShowlegal=true;
	}
	
	$content .= '<DIV CLASS="adultatom_link_wrap">';
	if ($buy_url ne '') {
		$content .= '<A CLASS="adultatom_link" HREF="'.$buy_url.'" TITLE="'.$buy_title.'">';
		if ($buy_image ne '') {
			$content .= '<IMG SRC="'.$buy_image.'" BORDER=0><BR/>';
		}
		$content .= $buy_title.' - ' . $title . '</A><BR/>';
	}
	if ($sponsor_url ne '') {
		$content .= '<A CLASS="adultatom_link" HREF="'.$sponsor_url.'" TITLE="'.$sponsor_title.'">More from: '.$sponsor_title.'</A><BR/>';
	}
	
	if ($otherlinks==1) {
		$other_title_cnt = @other_title;
		print "<BR/><b>Others</b><BR/>\n";
		for ($x=0;$x<$other_title_cnt;$x++) {		
			print "Title: " . $other_title[$x] . "<BR/>\n";
			print "URL: " . $other_url[$x] . "<BR/>\n";
			$content .= '<A CLASS="adultatom_other" ID="other_'.$video_id.'" NAME="other_'.$video_id.'" HREF="'.$other_url[$x].'" TITLE="'.$mpg_title[$x].'">'.$other_title[$x].'</A><BR/>';
		}
	}						
	$content .= "</DIV>";
	if ($twotwofiveseven_url ne '' && $bShowlegal) {
		$content .= '<BR/><DIV><A CLASS="adultatom_twotwofiveseven" HREF="'.$twotwofiveseven_url.'" TITLE="Title 18 &sect; 2257">Title 18 &sect; 2257</A></DIV>';
	}

	$content .= '<BR/><HR/>';

}

open(MAIN, "$scriptdir/main.html");
chomp(@main=<MAIN>);
close(MAIN);
$main_html=join("\n", @main);
$main_html =~ s/%scripturl%/$scripturl/gi;
$main_html =~ s/%content%/$content/gi;

open(OUT, ">$scriptdir/output.html");
print OUT $main_html;
close(OUT);

print "Done\n";

sub urlencode {
	my $theURL = $_[0];
	
	$theURL =~ s/\?/%3F/g;
	$theURL =~ s/\=/%3D/g;
	$theURL =~ s/\&/%26/g;
	#$theURL =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
	
	return $theURL;
}
